using System;
using System.Collections;
using System.Collections.Specialized;
using System.Xml;

using MediaWikiEngine.Domain;

namespace MediaWikiEngine.Command
{
	/// <summary>
	/// Summary description for SavePage.
	/// </summary>
	public class SavePage : BaseCommand
	{

		public SavePage(CommandParams parameters, WikiConnection connection) : base(parameters, connection)
		{
		}
        
        public SavePage(WikiConnection conn, WikiPage pagedata, string summary, string ns, string title) : base(conn)
        {
            CommandParams parameters = new CommandParams();
            parameters["pagedata"] = pagedata;
            parameters["summary"] = summary;
            if (string.Empty != ns)
                parameters["namespace"] = ns;
            if (string.Empty != title)
                parameters["title"] = title;

            _params = parameters;
        }

		/// <summary>
		/// Creates a Wiki Get Page command using an Xml text writer.
		/// </summary>
		/// <param name="inXW"></param>
		/// <param name="inNamespace"></param>
		/// <param name="inTitle"></param>
		static public void ToXml(
			XmlTextWriter inXW,
			string inNamespace,
			string inTitle,
			string inSummary)
		{
			StartCommandToXml(inXW, "wiki:savepage");

			StartConditionsToXml(inXW);
			IfConditionToXml(inXW, "$page", "isset", "bot:exit");
			EndConditionsToXml(inXW);

			StartInputToXml(inXW);
			InputNameValueToXml(inXW, "namespace", inNamespace);
			InputNameValueToXml(inXW, "title", inTitle);
			InputNameValueToXml(inXW, "pagedata", "$page");
			inXW.WriteStartElement("in");
			inXW.WriteAttributeString("name", "summary");
			inXW.WriteString(inSummary);
			inXW.WriteEndElement();	//	/in
			EndInputToXml(inXW);

			EndCommandToXml(inXW);
		}

		public override CommandResult Execute()
		{
			NameValueCollection formData = new NameValueCollection();

			WikiPage wp = _params["pagedata"] as WikiPage;
            if (true == _params.Contains("namespace"))
                wp.Namespace = (string) _params["namespace"];
            if (true == _params.Contains("title"))
                wp.Title = (string) _params["title"];

            wp.PrepareForSave();

			formData.Add("wpTextbox1", wp.WikiMarkup);
			formData.Add("wpSummary", _params["summary"].ToString());
			formData.Add("wpSave", "Save page");
			formData.Add("wpSection","");
            
            if (null != wp.Watch)
                formData.Add("wpWatchthis", wp.Watch.Value ? "1" : "0");

			formData.Add("wpEdittime", wp.GetInputFieldValue("wpEdittime"));
			formData.Add("wpEditToken", wp.GetInputFieldValue("wpEditToken"));

			_conn.DoPostForm(_conn.Endpoint.PageWithMode(wp.Namespace, wp.Title, "submit"), formData);

            wp.Exists = true;

            Hashtable results = new Hashtable();
            results["pagedata"] = wp;
			return new SavePageResult(results);
		}
	}

    public class SavePageResult : CommandResult
    {
        public SavePageResult(Hashtable results)
            : base(results)
        {
        }

        public WikiPage Page
        {
            get { return GetData<WikiPage>("pagedata"); }
        }
    }
}
